import sys
import requests as requests


## 功能：获取城市天气情况
## 用法：python ./getWeather.py [中文城市名称] ## 城市要写中文


## 获取API中的数据
def getApiWeather(city):
    cityMsg = ''
    if city != '/':
        cityMsg = '?city=' + city

    # get weather data
    resp = requests.get('https://api.vvhan.com/api/weather%s' % cityMsg)

    return resp.json()


def formatMsg(city):
    print(city)
    weather = getApiWeather(city)
    city = weather['city']
    info = weather['info']
    night = weather['info']['night']
    air = weather['info']['air']
    tip = weather['info']['tip']
    msg = """
        当前城市：%s
        日期：%s
        星期：%s
        阴晴：%s
        气温：%s  %s
        风向：%s
        风力：%s
        夜间：%s
        夜间风向：%s
        夜间风力：%s
        空气质量指数：%s
        空气质量等级：%s %s
        一氧化碳浓度：%s
        二氧化氮浓度：%s
        臭氧浓度：%s
        PM10浓度：%s
        PM2.5浓度：%s
        二氧化硫浓度：%s
        温馨提示：%s
        """ % (
        city, info['date'], info['week'], info['type'], info['high'], info['low'], info['fengxiang'], info['fengli'],
        night['type'], night['fengxiang'], night['fengli'], air['aqi'], air['aqi_level'], air['aqi_name'], air['co'],
        air['no2'], air['o3'], air['pm10'], air['pm2.5'], air['so2'], tip)
    print(msg)
    return msg


if __name__ == '__main__':
    ## 如果用户选择城市，就返回该城市数据，不写或格式不对的按IP返回所在城市数据
    if len(sys.argv) == 2:
        city = sys.argv[1]
    else:
        city = ''
    msg = formatMsg(city)

## 接收json数据格式如下：
"""
{"success":true,
"city":"北海市",
"info":{
    "date":"2023-01-28",
    "week":"星期六",
    "type":"晴",
    "high":"最高 16°C",
    "low":"最低 7°C",
    "fengxiang":"北风",
    "fengli":"5级",
    "night":{
        "type":"晴",
        "fengxiang":"北风",
        "fengli":"5级"},
    "air":{
        "aqi":52,
        "aqi_level":2,
        "aqi_name":"良",
        "co":"0",
        "no2":"4",
        "o3":"69",
        "pm10":"54",
        "pm2.5":"31",
        "so2":"5"},
    "tip":"你若安好，便是晴天~ 天有点冷，注意保暖~"
    }
}
"""
